package com.caterina.activityManager;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.LayoutManager;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.HierarchyBoundsListener;
import java.awt.event.HierarchyEvent;
import java.util.ArrayList;
import java.util.Collection;

import javax.swing.JLayeredPane;

import modulesAPI.v2.Module;

import com.caterina.components.CAlphaPanel;
import com.caterina.components.CButton;
import com.caterina.components.CLoading;
import com.caterina.components.COptionPane;
import com.caterina.components.COptionPane.MessageType;
import com.caterina.components.CPanel;
import com.caterina.resources.CWords;

import controllers.AsyncController;
import controllers.DataListener;
import controllers.ManagerTask;
import controllers.ManagerTask.TaskType;

public abstract class CActivity extends JLayeredPane implements DataListener,HierarchyBoundsListener{
	public CActivity (String name,Container parent){
		this.title=CWords.getWord(name);
		this.name=name;
		this.parent=parent;
		this.addHierarchyBoundsListener(this);
		this.createPanels();
		this.startLoading();
		this.setMinimumSize(new Dimension(400,400));
	}
	public abstract void onStart();
	public void onStop(){}
	
	public void startLoading(){
		loadPanel.setVisible(true);
		this.refresh();
	}
	public void close(String status){
		if(this.getParentActivity()!=null)
			this.getParentActivity().closeOptionPane(status);
	}
	public void stopLoading(){
		loadPanel.setVisible(false);
		this.refresh();
	}
	public void onOptionPaneClosed(String status){}
	public void setParent(Container parent){
		this.parent=parent;
	}
	public Component add(Component comp){
		contentPanel.removeAll();
		contentPanel.add(comp,"DEFAULT");
		if(this.smartLoading)this.stopLoading();
		this.refresh();
		return comp;
	}
	public void onOptionPaneButtonPressed(CButton button){
		if (button.getText().equalsIgnoreCase(CWords.getWord("MESSAGE_OK_BUTTON"))){
			this.closeOptionPane("MESSAGE_OK_BUTTON");
		}
	}
	public void showCOPtionPane(MessageType type,CPanel contentPanel){
		this.showCOPtionPane(new COptionPane(type, contentPanel, this));
	}
	public void showCOPtionPane(MessageType type,Module<?> mod){
		this.showCOPtionPane(COptionPane.createModuleOptionPane(type, mod, this));
	}
	public void showCOPtionPane(CActivity activity){
		this.showCOPtionPane(COptionPane.createActivityOptionPane(activity, this));
	}
	public void showCOPtionPane(MessageType type,String text){
		this.showCOPtionPane(COptionPane.createTextOptionPane(type, text, this));
	}
	public void closeOptionPane(String status){
		this.popupPanel.setVisible(false);
		this.setEnabled(true);
		this.popupPanel.removeAll();
		this.onOptionPaneClosed(status);
	}
	public void showCOPtionPane(COptionPane optionPane){
		this.stopLoading();
		this.setEnabled(false);
		this.popupPanel.removeAll();
		this.popupPanel.add(optionPane,BorderLayout.CENTER);
		this.popupPanel.setVisible(true);
	}
	@Override
	public void setEnabled(boolean enabled) {this.modalPanel.setVisible(!enabled);this.refresh();}
	public void add(Module<?> module){this.add(module.getPanel());}
	
	public void showToast(){
		this.modalPanel.setVisible(true);
		this.popupPanel.setVisible(true);
	}
	
	private void createPanels(){
		cl=new CardLayout();
		this.contentPanel=new CPanel(cl);
		this.loadPanel=new CPanel(new BorderLayout());;
		this.modalPanel=new CAlphaPanel();
		this.popupPanel=new CPanel(new BorderLayout());

		this.setBounds(this.getBounds());
		
		this.contentPanel.setVisible(true);
		this.loadPanel.setVisible(false);
		this.modalPanel.setVisible(false);
		this.popupPanel.setVisible(false);
		this.popupPanel.setOpaque(false);
		
		loadPanel.add(load,BorderLayout.CENTER);
		
		super.add(loadPanel,LOAD_LEVEL);
		super.add(popupPanel,POPUP_LEVEL);
		super.add(modalPanel,MODAL_LEVEL);
		super.add(contentPanel,CONTENT_LEVEL);
	}
	
	@Override
	public Rectangle getBounds() {
		
		if(this.getParentActivity()==null){
			Rectangle tmpBounds=ActivityManager.getFrameBounds();
			String os=System.getProperty("os.name");
			if(os.contains("Mac")) return new Rectangle(new Point(MAC_X,MAC_Y),new Dimension(tmpBounds.width+MAC_W,tmpBounds.height+MAC_H));
			else return new Rectangle(new Point(WIN_X,WIN_Y),new Dimension(tmpBounds.width+WIN_W,tmpBounds.height+WIN_H));
		}else return super.getBounds();
	}
	public void refresh(){
		this.setBounds(this.getBounds());
		this.revalidate();
		this.repaint();
	}
	@Override
	public void setBounds(Rectangle r) {
		this.contentPanel.setBounds(r);
		this.loadPanel.setBounds(r);
		this.modalPanel.setBounds(r);
		this.popupPanel.setBounds(r);
		super.setBounds(r);
	}
	public void revalidate(){
		this.contentPanel.revalidate();
		this.modalPanel.revalidate();
		this.popupPanel.revalidate();
		super.revalidate();
	}
	public void repaint(){
		this.contentPanel.repaint();
		this.modalPanel.repaint();
		this.popupPanel.repaint();
		super.repaint();
	}
	public Container getParentContainer(){
		return this.parent;
	}
	public CActivity getParentActivity(){
		if(this.parent instanceof CActivity){
			return (CActivity) this.parent;
		}else return null;
	}
	public void addAbstractActivityButton(String buttonText){this.abstractActivityButtons.add(buttonText);}
	public void removeAbstractActivityButton(String buttonText){this.abstractActivityButtons.remove(buttonText);}
	public Collection<String> getAbstractActivityButton(){return this.abstractActivityButtons;}
	
	public void abortAll(TaskType taskType){this.activityManager.abortAll(taskType);}
	public void putTask(TaskType type,Object...args){this.activityManager.putTask(new ManagerTask(type,this,args));}
	public boolean isCBarVisible(){return this.cBarVisible;}
	public boolean isCBarQueryesEnabled(){return this.cBarQueryes;}	
	public boolean isLogoutButtonVisible(){return this.logoutVisible;}
	public boolean isSbButtonVisible(){return this.sbVisible;}
	public boolean isCBarLogoButtonEnabled(){return this.cBarLogoButtonEnabled;}
	public boolean isCSbBarvisible(){return barVisible;}
	public void setSmartLoadingEnabled(boolean enabled){this.smartLoading=enabled;}
	public String getName(){return this.name;}
	public AsyncController getDataController(){return this.getActivityManager().getDataController();}
	public ActivityManager getActivityManager(){return this.activityManager;}
	public String getTitle(){return this.title;}
	public void setSbButtonVisible(boolean isVisible){this.sbVisible=isVisible;}
	public void setLayout(LayoutManager man){}
	public void setActivityManager(ActivityManager activityManager){this.activityManager=activityManager;}
	public void setLogoutButtonVisible(boolean isVisible){this.logoutVisible=isVisible;}
	public void setCBarVisible(Boolean visible){this.cBarVisible=visible;}
	public void setCSbBarvisible(boolean isVisible){this.barVisible=isVisible;}
	public void setTitle(String title){this.title=CWords.getWord(title);}
	public void setCBarQueryesEnabled(boolean enabled){this.cBarQueryes=enabled;}
	
	private boolean smartLoading=true;
	private boolean cBarLogoButtonEnabled=true;
	private ArrayList<String> abstractActivityButtons=new ArrayList<String>();
	private boolean barVisible=false;
	private String name;
	private ActivityManager activityManager;
	private boolean cBarVisible=true;
	private boolean logoutVisible=true;
	private boolean sbVisible=true;
	private boolean cBarQueryes=true;
	private CLoading load=new CLoading(0);
	private static final Integer LOAD_LEVEL=1,CONTENT_LEVEL=0,MODAL_LEVEL=2,POPUP_LEVEL=3;
	private CPanel contentPanel,loadPanel,modalPanel,popupPanel;
	private String title;
	private static int WIN_W=-10,WIN_H=-71,WIN_X=0,WIN_Y=0;
	private static int MAC_W=0,MAC_H=-51,MAC_X=0,MAC_Y=0;
	private CardLayout cl;
	private Container parent;
	@Override
	public void ancestorMoved(HierarchyEvent e) {}
	@Override
	public void ancestorResized(HierarchyEvent e) {this.refresh();}
	private static final long serialVersionUID = 1L;
}
